home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / test / test11.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  2KB  |  128 lines

  1. /* test 11 */
  2.  
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7.  
  8. #define MAX_ERROR 1
  9.  
  10. char *envp[3] = {"spring", "summer", 0};
  11.  
  12. extern errno;
  13. int errct, subtest;
  14.  
  15. main()
  16. {
  17.   int i;
  18.  
  19.   printf("Test 11 ");
  20.   fflush(stdout);        /* have to flush for child's benefit */
  21.  
  22.   if (geteuid() != 0) {
  23.     printf("must be setuid root; test aborted\n");
  24.     exit(1);
  25.   }
  26.  
  27.   for (i = 0; i < 9; i++) {
  28.     test11a();
  29.     test11b();
  30.   }
  31.   if (errct == 0)
  32.     printf("ok\n");
  33.   else
  34.     printf(" %d errors\n", errct);
  35.   exit(0);
  36. }
  37.  
  38.  
  39. test11a()
  40. {
  41. /* Test exec */
  42.   int n, fd, fd1, i;
  43.   char aa[4];
  44.  
  45.  
  46.   subtest = 1;
  47.  
  48.   if (fork()) {
  49.     wait(&n);
  50.     if (n != 25600) e(1);
  51.     unlink("t1");
  52.     unlink("t2");
  53.   } else {
  54.     if (chown("t11a", 10, 20) < 0) e(2);
  55.     chmod("t11a", 0666);
  56.  
  57.     /* The following call should fail because the mode has no X
  58.      * bits on. If a bug lets it unexpectedly succeed, the child
  59.      * will print an error message since the arguments are wrong. */
  60.     execl("t11a", (char *) 0, envp);    /* should fail -- no X
  61.                          * bits on */
  62.  
  63.     /* Control should come here after the failed execl(). */
  64.     chmod("t11a", 06555);
  65.     if ((fd = creat("t1", 0600)) != 3) e(3);
  66.     if (close(fd) < 0) e(4);
  67.     if (open("t1", O_RDWR) != 3) e(5);
  68.     if (chown("t1", 10, 99) < 0) e(6);
  69.     if ((fd = creat("t2", 0060)) != 4) e(7);
  70.     if (close(fd) < 0) e(8);
  71.     if (open("t2", O_RDWR) != 4) e(9);
  72.     if (chown("t2", 99, 20) < 0) e(10);
  73.     if (setgid(6) < 0) e(11);
  74.     if (setuid(5) < 0) e(12);
  75.     if (getuid() != 5) e(13);
  76.     if (geteuid() != 5) e(14);
  77.     if (getgid() != 6) e(15);
  78.     if (getegid() != 6) e(16);
  79.     aa[0] = 3;
  80.     aa[1] = 5;
  81.     aa[2] = 7;
  82.     aa[3] = 9;
  83.     if (write(3, aa, 4) != 4) e(17);
  84.     lseek(3, 2L, 0);
  85.     execle("t11a", "t11a", "arg0", "arg1", "arg2", (char *) 0, envp);
  86.     e(18);
  87.     printf("Can't exec t11a\n");
  88.     exit(3);
  89.   }
  90. }
  91.  
  92.  
  93.  
  94. test11b()
  95. {
  96.   int n;
  97.   char *argv[5];
  98.  
  99.   subtest = 2;
  100.   if (fork()) {
  101.     wait(&n);
  102.     if (n != (75 << 8)) e(20);
  103.   } else {
  104.     /* Child tests execv. */
  105.     argv[0] = "t11b";
  106.     argv[1] = "abc";
  107.     argv[2] = "defghi";
  108.     argv[3] = "j";
  109.     argv[4] = 0;
  110.     execv("t11b", argv);
  111.     e(19);
  112.   }
  113. }
  114.  
  115. e(n)
  116. int n;
  117. {
  118.   int err_num = errno;        /* save errno in case printf clobbers it */
  119.  
  120.   printf("Subtest %d,  error %d  errno=%d  ", subtest, n, errno);
  121.   errno = err_num;        /* restore errno, just in case */
  122.   perror("");
  123.   if (errct++ > MAX_ERROR) {
  124.     printf("Too many errors; test aborted\n");
  125.     exit(1);
  126.   }
  127. }
  128.